<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-hemisu.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-hemisu.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-hemisu.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="读书笔记,js高程,">










<meta name="description" content="[TOC] 本章内容 理解包含不同层次节点的DOM 使用不同的节点类型 克服浏览器兼容性问题及各种陷阱">
<meta name="keywords" content="读书笔记,js高程">
<meta property="og:type" content="article">
<meta property="og:title" content="js高程读书笔记 第十章 DOM">
<meta property="og:url" content="http://www.hemisu.com/2017/08/13/294/index.html">
<meta property="og:site_name" content="何米酥`s Blog">
<meta property="og:description" content="[TOC] 本章内容 理解包含不同层次节点的DOM 使用不同的节点类型 克服浏览器兼容性问题及各种陷阱">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-08-14T15:20:57.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="js高程读书笔记 第十章 DOM">
<meta name="twitter:description" content="[TOC] 本章内容 理解包含不同层次节点的DOM 使用不同的节点类型 克服浏览器兼容性问题及各种陷阱">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://www.hemisu.com/2017/08/13/294/">





  <title>js高程读书笔记 第十章 DOM | 何米酥`s Blog</title>
  








  <!-- Hotjar Tracking Code for www.hemisu.com -->
  <script>
      (function(h,o,t,j,a,r){
          h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
          h._hjSettings={hjid:1933736,hjsv:6};
          a=o.getElementsByTagName('head')[0];
          r=o.createElement('script');r.async=1;
          r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
          a.appendChild(r);
      })(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');
  </script>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">何米酥`s Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">EFE</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.hemisu.com/2017/08/13/294/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="何米酥">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="何米酥`s Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">js高程读书笔记 第十章 DOM</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-13T15:51:00+00:00">
                2017-08-13
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/笔记/" itemprop="url" rel="index">
                    <span itemprop="name">笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>[TOC]</p>
<h1 id="本章内容"><a href="#本章内容" class="headerlink" title="本章内容"></a>本章内容</h1><ul>
<li>理解包含不同层次节点的DOM</li>
<li>使用不同的节点类型</li>
<li>克服浏览器兼容性问题及各种陷阱</li>
</ul>
<a id="more"></a>
<h1 id="节点层次"><a href="#节点层次" class="headerlink" title="节点层次"></a>节点层次</h1><p>节点之间的关系构成了层次，而所有页面标记则表现为一个以特定节点为根节点的树形结构。</p>
<p>文档节点是每个文档的根节点。</p>
<h1 id="Node类型"><a href="#Node类型" class="headerlink" title="Node类型"></a>Node类型</h1><p>节点类型由Node类型中定义数值常量表示，一般用的较多的是</p>
<ul>
<li>Node.ELEMENT_NODE(1);</li>
<li>Node.TEXT_NODE(3);</li>
</ul>
<p>对于元素节点，nodeName中保存的始终都是元素的标签名，而nodeValue的值则始终为null。</p>
<p>每个节点都有一个childNodes属性，保存着一个NodeList对象。NodeList是一种类数组对象，用于保存一组有序的节点，但它不是Array实例，是基于DOM结构动态执行查询的结果，因此DOM的结构变化能够自动反应在NodeList对象中。</p>
<p>以下代码将NodeList转化为数组<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">convertToArray</span>(<span class="params">nodes</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> array = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">try</span>&#123;</span><br><span class="line">        array = <span class="built_in">Array</span>.prototype.slice.call(nodes, <span class="number">0</span>);<span class="comment">//对非IE浏览器有效</span></span><br><span class="line">    &#125; <span class="keyword">catch</span> (ex) &#123;</span><br><span class="line">        array = <span class="keyword">new</span> <span class="built_in">Array</span>();</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>, len = nodes.length;i &lt; len; i++)&#123;</span><br><span class="line">            array.push(nodes[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>每一个节点都有一个parentNode属性，该属性指向文档树中的父节点。</p>
<p>previousSibling和nextSibling属性可以访问同胞节点</p>
<p>firstChild和lastChild的值始终等于someNode.childNodes[0]和someNode.childNodes[someNode.childNodes.length - 1]</p>
<p>hasChildNodes()方法在节点包含一或多个子节点的情况下返回true;</p>
<p>所有节点都有最后一个属性是ownerDocument，该属性指向表示整个文档的文档节点。</p>
<p>appendChild()方法用于想childNodes列表的末尾添加一个节点。如果传入到appendChild()中的节点已经是文档的一部分了，那结果就是将该节点从原来的位置转移到新位置。</p>
<p>如果要把节点放在childNodes列表中某个特定的位置上，而不是放在末尾，那么可以使用insetBefore()方法。这个方法接收两个参数：要插入的节点和作为参照的节点。如果参照的节点是null，那么就会有和appendChild()一样的操作<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//插入后成为最后一个子节点</span></span><br><span class="line">returnedNode = someNode.insertBefore(newNode, <span class="literal">null</span>);</span><br><span class="line"><span class="built_in">console</span>.log(newNode == someNode.lastChild);<span class="comment">//true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//插入后成为第一个子节点</span></span><br><span class="line">returnedNode = someNode.insertBefore(newNode,someNode.firstChild);</span><br><span class="line"><span class="built_in">console</span>.log(newNode == someNode.firstChild);<span class="comment">//true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//插入最后一个子节点前面</span></span><br><span class="line">returnedNode = someNode.insetBefore(newNode, someNode.lastChild);</span><br><span class="line"><span class="built_in">console</span>.log(newNode == someNode.childNodes[someNode.length - <span class="number">2</span>]);</span><br></pre></td></tr></table></figure></p>
<p>replaceChild()方法接收两个参数：要插入的节点和要替换的节点。要替换的节点将由这个方法返回并从文档树中被移除，同时由要插入的节点占据其位置。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//替换第一个子节点</span></span><br><span class="line"><span class="keyword">var</span> returnedNode = someNode.replaceChild(newNode, someNode.firstChild);</span><br><span class="line"></span><br><span class="line"><span class="comment">//替换最后一个子节点</span></span><br><span class="line"><span class="keyword">var</span> returnedNode = someNode.replaceChild(newNode, someNode.lastChild);</span><br></pre></td></tr></table></figure></p>
<p>removeNode()方法接收一个参数，即要移除的节点<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//移除第一个子节点</span></span><br><span class="line"><span class="keyword">var</span> formerFirstChild = someNode.removeChild(someNode.firstChild);</span><br><span class="line"></span><br><span class="line"><span class="comment">//移除最后一个子节点</span></span><br><span class="line"><span class="keyword">var</span> formerLastChild = someNode.removeChild(someNode.lastChild);</span><br></pre></td></tr></table></figure></p>
<p>cloneNode()接收一个布尔值参数表示是否执行深复制。参数为true的情况下执行深复制，也就是复制节点及其整个子节点数；</p>
<h2 id="Document类型-9"><a href="#Document类型-9" class="headerlink" title="Document类型 9"></a>Document类型 9</h2><p>Document对象是window对象的一个属性。</p>
<p>以下三种方式可以获得<code>&lt;html&gt;</code>元素<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> html = <span class="built_in">document</span>.documentElement;<span class="comment">//取得对&lt;html&gt;的引用</span></span><br><span class="line"><span class="built_in">console</span>.log(html === <span class="built_in">document</span>.childNodes[<span class="number">1</span>]);<span class="comment">//true</span></span><br><span class="line"><span class="built_in">console</span>.log(html === <span class="built_in">document</span>.firstElementChild);<span class="comment">//true</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> body = <span class="built_in">document</span>.body;<span class="comment">//取得对body的引用</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> doctype = <span class="built_in">document</span>.doctype;<span class="comment">//取得对doctype的引用</span></span><br></pre></td></tr></table></figure></p>
<h3 id="文档信息"><a href="#文档信息" class="headerlink" title="文档信息"></a>文档信息</h3><p>通过title这个属性可以取得当前页面的标题也可以修改当前页面的标题。修改title属性的值不会改变<code>&lt;title&gt;</code>元素<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//取得文档标题</span></span><br><span class="line"><span class="keyword">var</span> originalTitle = <span class="built_in">document</span>.title;</span><br><span class="line"></span><br><span class="line"><span class="comment">//设置文档标题</span></span><br><span class="line"><span class="built_in">document</span>.title = <span class="string">"New page title"</span>;</span><br></pre></td></tr></table></figure></p>
<p>URL、domain和referrer<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//获取完整的URL</span></span><br><span class="line"><span class="keyword">var</span> url = <span class="built_in">document</span>.URL;</span><br><span class="line"></span><br><span class="line"><span class="comment">//取域名</span></span><br><span class="line"><span class="keyword">var</span> domain = <span class="built_in">document</span>.domain;</span><br><span class="line"></span><br><span class="line"><span class="comment">//取得来源页面的URL</span></span><br><span class="line"><span class="keyword">var</span> referrer = <span class="built_in">document</span>.referrer;</span><br></pre></td></tr></table></figure></p>
<p>这三个属性中，只有domain是可以设置的，但是不能将这个属性设置为URL中不包含的域。</p>
<p>如果域名一开始是松散的(loose)，那么不能将它再设置为紧绷的(tight)。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//假设页面来自于p2p.wrox.com域</span></span><br><span class="line"><span class="built_in">document</span>.domain = <span class="string">"wrox.com"</span>;<span class="comment">//成功</span></span><br><span class="line"><span class="built_in">document</span>.domain = <span class="string">"pap.wrox.com"</span><span class="comment">//出错</span></span><br></pre></td></tr></table></figure></p>
<h3 id="查找元素"><a href="#查找元素" class="headerlink" title="查找元素"></a>查找元素</h3><p>gerElementById();接收一个参数：要取得的元素的ID，返回文档中第一次出现的元素</p>
<p>getElementsByTagName();接收一个参数：要取得元素的标签名，返回包含零或多个元素的NodeList。在HTML文档中，这个方法会返回一个HTMLCollection对象，作为一个“动态”集合。<br>HTMLCollection对象还有一个方法，叫做namedItem()，这个方法可以通过元素的name特性取得集合中的项。<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">"myimage.gif"</span> <span class="attr">name</span>=<span class="string">"myImage"</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>这样就可以用下如下方式取得这个<code>&lt;img&gt;</code>元素<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> images = <span class="built_in">document</span>.getElementsByTagName(<span class="string">"img"</span>);</span><br><span class="line"><span class="keyword">var</span> myImage = images.namedItem(<span class="string">"myImage"</span>);</span><br></pre></td></tr></table></figure></p>
<p>要取得文档中的所有元素<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> allElements = <span class="built_in">document</span>.getElementsByTagName(<span class="string">"*"</span>);</span><br></pre></td></tr></table></figure></p>
<p>getElementsByName()方法，如果对单选按钮radio使用，namedItem()方法只会取得第一项。</p>
<h3 id="特殊方法"><a href="#特殊方法" class="headerlink" title="特殊方法"></a>特殊方法</h3><ul>
<li>document.anchors 包含文档中所有带name特性的<code>&lt;a&gt;</code>元素</li>
<li>document.forms 包含文档中所有<code>&lt;form&gt;</code>元素，与<code>document.getElementsByTagName(&quot;form&quot;)</code>效果相同</li>
<li>document.images 包含文档中所有<code>&lt;img&gt;</code>元素，与<code>document.getElementsByTagName(&quot;img&quot;)</code>效果相同</li>
<li>document.links 包含文档中所有带href特性的<code>&lt;a&gt;</code>元素</li>
</ul>
<h3 id="dom一致性检测"><a href="#dom一致性检测" class="headerlink" title="dom一致性检测"></a>dom一致性检测</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> hasXmlDom = <span class="built_in">document</span>.implementation.hasFeature(<span class="string">"XML"</span>, <span class="string">"1.0"</span>);</span><br></pre></td></tr></table></figure>
<h3 id="文档写入"><a href="#文档写入" class="headerlink" title="文档写入"></a>文档写入</h3><p>document.write()会原样写入。document.writeln()会在字符串的末尾添加一个换行符(<code>\n</code>)</p>
<h2 id="Element类型-1"><a href="#Element类型-1" class="headerlink" title="Element类型 1"></a>Element类型 1</h2><p>要访问元素的标签名，可以使用nodeName属性也可以使用tagName属性。后者更清晰。<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"myDiv"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> div = <span class="built_in">document</span>.getElementById(<span class="string">"myDiv"</span>);</span><br><span class="line"><span class="built_in">console</span>.log(div.tagName);</span><br><span class="line"><span class="built_in">console</span>.log(div.tagName == div.nodeName);</span><br></pre></td></tr></table></figure>
<p>在HTML中，标签名始终都以全部大写显示；在XML中会与源代码中的保持一致。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">element.tagName.toLowerCase()</span><br></pre></td></tr></table></figure></p>
<p>通过这样转化一下比较好</p>
<h3 id="HTML元素"><a href="#HTML元素" class="headerlink" title="HTML元素"></a>HTML元素</h3><h3 id="取得特性"><a href="#取得特性" class="headerlink" title="取得特性"></a>取得特性</h3><ul>
<li>getAttribute()</li>
<li>setAttribute()</li>
<li>removeAttribute()</li>
</ul>
<p>根据THML5规范，自定义特性应该加上data-前缀以便验证。</p>
<p>有两类特殊的特性，虽然有对应的属性名，但属性的值与通过getAttribute()返回的值并不相同。</p>
<p>style通过getAttribute()返回的是css文本，而通过属性来访问则会返回一个对象。</p>
<p>onlick事件处理程序用getAttribute()访问，会返回相应代码的字符串，用onlick属性访问时，会返回一个JavaScript函数。</p>
<h3 id="attributes属性"><a href="#attributes属性" class="headerlink" title="attributes属性"></a>attributes属性</h3><p>包含一个NamedNodeMap，与NodeList类似是一个动态的集合。</p>
<p>attributes属性中包含一系列节点，每个节点的nodeName就是特性的名称，而节点的nodeValue就是特性的值。要取得元素的id特性<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> id = element.attributes.getNamedItem(<span class="string">"id"</span>).nodeValue;</span><br><span class="line"><span class="comment">//也可以使用</span></span><br><span class="line"><span class="keyword">var</span> id = element.attributes[<span class="string">"id"</span>].nodeValue;</span><br></pre></td></tr></table></figure></p>
<p>removeAttribute()与attributes属性中的removeNamedItem()方法唯一的区别就在于后者会返回被删除的Attr节点</p>
<h3 id="创建元素"><a href="#创建元素" class="headerlink" title="创建元素"></a>创建元素</h3><p>可以在createElement()中指定完整的HTML标签来解决IE7及更早版本的问题<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(client.browser.ie &amp;&amp; client.rowser.ie &lt;=<span class="number">7</span>)&#123;</span><br><span class="line">    <span class="comment">//创建一个带name特性的iframe元素</span></span><br><span class="line">    <span class="keyword">var</span> iframe = <span class="built_in">document</span>.createElement(<span class="string">"&lt;iframe name=\"myframe\"&gt;&lt;iframe&gt;"</span>);</span><br><span class="line">    <span class="comment">//创建input元素</span></span><br><span class="line">    <span class="keyword">var</span> input = <span class="built_in">document</span>.createElement(<span class="string">"&lt;input type=\"checkbox\"&gt;"</span>);</span><br><span class="line">    <span class="comment">//创建button元素</span></span><br><span class="line">    <span class="keyword">var</span> button = <span class="built_in">document</span>.createElement(<span class="string">"&lt;button type=\"reset\"&gt;&lt;/button&gt;"</span>);</span><br><span class="line">    <span class="comment">//创建单选按钮</span></span><br><span class="line">    <span class="keyword">var</span> radio1 = <span class="built_in">document</span> = createElement(<span class="string">"&lt;input type=\"radio\" name=\"choice\" value=\"1\""</span>);</span><br><span class="line">    <span class="keyword">var</span> radio2 = <span class="built_in">document</span> = createElement(<span class="string">"&lt;input type=\"radio\" name=\"choice\" value=\"2\""</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="元素的子节点"><a href="#元素的子节点" class="headerlink" title="元素的子节点"></a>元素的子节点</h3><p>元素可以有任意数目的子节点和后代节点，因为元素可以是其他元素的子节点。在IE中，对下列代买解析：<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">"myList"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>Item 1<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>Item 2<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>Item 3<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>会获得3个<code>&lt;li&gt;</code>元素和4个文本节点(空白符)</p>
<p>意味着在执行某项操作之前，通常都要先检查一下nodeType属性。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>, len = element.childNodes.length; i &lt; len ;i++)&#123;</span><br><span class="line">    <span class="keyword">if</span>(element.childNodes[i].nodeType == <span class="number">1</span>)&#123;</span><br><span class="line">        <span class="comment">//执行某些操作</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="text类型-3"><a href="#text类型-3" class="headerlink" title="text类型 3"></a>text类型 3</h2><p>修改文本节点的内容时，内容会被html编码</p>
<h3 id="创建文本节点"><a href="#创建文本节点" class="headerlink" title="创建文本节点"></a>创建文本节点</h3><p>document.createTextNode()创建新文本节点。</p>
<h3 id="规范化文本节点"><a href="#规范化文本节点" class="headerlink" title="规范化文本节点"></a>规范化文本节点</h3><p>normalize()方法可以将所有文本节点合并成一个节点</p>
<h3 id="分割文本节点"><a href="#分割文本节点" class="headerlink" title="分割文本节点"></a>分割文本节点</h3><p>splitText()方法可以将文本分成两部分，原来的文本节点将会包含从开始到指定位置之前的内容，新文本节点将包含剩下的文本。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> element = <span class="built_in">document</span>.createElement(<span class="string">"div"</span>);</span><br><span class="line">element.className = <span class="string">"message"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> textNode = <span class="built_in">document</span>.createTextNode(<span class="string">"Hello world!"</span>);</span><br><span class="line">element.appendChild(textNode);</span><br><span class="line"></span><br><span class="line"><span class="built_in">document</span>.body.appendChild(element);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> newNode = element.firstChild.splitText(<span class="number">5</span>);</span><br><span class="line"><span class="built_in">console</span>.log(element.firstChild.nodeValue);<span class="comment">//Hello</span></span><br><span class="line"><span class="built_in">console</span>.log(newNode.nodeValue);<span class="comment">// world!</span></span><br><span class="line"><span class="built_in">console</span>.log(element.childNodes.length);<span class="comment">//2</span></span><br></pre></td></tr></table></figure></p>
<h2 id="comment类型-8"><a href="#comment类型-8" class="headerlink" title="comment类型 8"></a>comment类型 8</h2><h2 id="CDATASection-4"><a href="#CDATASection-4" class="headerlink" title="CDATASection 4"></a>CDATASection 4</h2><h2 id="DocumentType-10"><a href="#DocumentType-10" class="headerlink" title="DocumentType 10"></a>DocumentType 10</h2><p>document.doctype中 DOM1级描述了三个属性：name、entities和notations；分别代表文档类型的名称；entities是由文档类型描述的实体NamedNodeMap对象；notations是由文档类型描述的符号的NamedNodeMap对象。</p>
<h2 id="DocumentFragment类型-11"><a href="#DocumentFragment类型-11" class="headerlink" title="DocumentFragment类型 11"></a>DocumentFragment类型 11</h2><p>可以包含和控制节点，把它作为一个“仓库”来使用，即可以在里面保存将来可能会添加到文档中的节点。<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">"myList"</span>&gt;</span><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>假设为这个<code>&lt;ul&gt;</code>元素添加3个列表项。如果逐个地添加列表项，将会导致浏览器反复渲染新信息。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> fragment = <span class="built_in">document</span>.createDocumentFragment();</span><br><span class="line"><span class="keyword">var</span> ul = <span class="built_in">document</span>.getElementById(<span class="string">"myList"</span>);</span><br><span class="line"><span class="keyword">var</span> li = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span> ; i++)&#123;</span><br><span class="line">    li = <span class="built_in">document</span>.createElement(<span class="string">"li"</span>);</span><br><span class="line">    li.appendChild(<span class="built_in">document</span>.createTextNode(<span class="string">"Item "</span> + (i+<span class="number">1</span>)));</span><br><span class="line">    fragment.appendChild(li);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ul.appendChild(fragment);</span><br></pre></td></tr></table></figure></p>
<h2 id="attr类型-2"><a href="#attr类型-2" class="headerlink" title="attr类型 2"></a>attr类型 2</h2><p>attr对象有3个属性：name、value和specified</p>
<p>其中specified用于区别特定是代码中指定的还是默认的。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> attr = <span class="built_in">document</span>.createAttribute(<span class="string">"align"</span>);</span><br><span class="line">attr.value = <span class="string">"left"</span>;</span><br><span class="line">element.setAttributeNode(attr);</span><br><span class="line"><span class="built_in">console</span>.log(element.attributes[<span class="string">"align"</span>].value));</span><br><span class="line"><span class="built_in">console</span>.log(element.getAttributeNode(<span class="string">"align"</span>).value);</span><br><span class="line"><span class="built_in">console</span>.log(element.getAttribute(<span class="string">"align"</span>));</span><br></pre></td></tr></table></figure>
<h1 id="DOM操作技术"><a href="#DOM操作技术" class="headerlink" title="DOM操作技术"></a>DOM操作技术</h1><h2 id="动态脚本"><a href="#动态脚本" class="headerlink" title="动态脚本"></a>动态脚本</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> script = <span class="built_in">document</span>.createElement(<span class="string">"script"</span>);</span><br><span class="line">script.type = <span class="string">"text/javascript"</span>;</span><br><span class="line"><span class="keyword">var</span> code = <span class="string">"function sayHi()&#123;alert('hi');&#125;"</span>;</span><br><span class="line"><span class="keyword">try</span>&#123;</span><br><span class="line">    script.appendChild(<span class="built_in">document</span>.createTextNode(<span class="string">"code"</span>));</span><br><span class="line">&#125; <span class="keyword">catch</span> (ex) &#123;</span><br><span class="line">    script.text = <span class="string">"code"</span>;<span class="comment">//在IE中,script元素是一个特殊对象，不允许DOM访问子节点，不过可以使用text的属性来指定</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">document</span>.body.appendChild(script);</span><br></pre></td></tr></table></figure>
<h2 id="动态样式"><a href="#动态样式" class="headerlink" title="动态样式"></a>动态样式</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">loadStyles</span>(<span class="params">url</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> link = <span class="built_in">document</span>.createElement(<span class="string">"link"</span>);</span><br><span class="line">    link.href = url;</span><br><span class="line">    link.rel = <span class="string">"stylesheet"</span>;</span><br><span class="line">    link.type = <span class="string">"text/css"</span>;</span><br><span class="line">    <span class="keyword">var</span> head = <span class="built_in">document</span>.getElementByTagName(<span class="string">"head"</span>)[<span class="number">0</span>];</span><br><span class="line">    head.appendChild(link);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="操作表格-p283"><a href="#操作表格-p283" class="headerlink" title="操作表格 p283"></a>操作表格 p283</h2><h2 id="使用NodeList"><a href="#使用NodeList" class="headerlink" title="使用NodeList"></a>使用NodeList</h2><p>NodeList和NamedNodeMap和HTMLCollection都是动态的，每当文档结构发生变化时，它们都会得到更新。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> divs = <span class="built_in">document</span>.getElementsByTagName(<span class="string">"div"</span>;),</span><br><span class="line">    i,</span><br><span class="line">    div;</span><br><span class="line"><span class="keyword">for</span>(i = <span class="number">0</span> ; i &lt; divs.length; i++)&#123;</span><br><span class="line">    div = <span class="built_in">document</span>.createElement(<span class="string">"div"</span>);</span><br><span class="line">    <span class="built_in">document</span>.body.appendChild(div);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>以上代码会导致无限循环，循环体每次都会增加一个div元素到文档中，而文档的长度每次循环后都会增加，结果i永远不能达到divs.length</p>
<p>所以如果要想迭代一个NodeList，最好是使用length属性初始化第二个变量，然后将迭代器与该变量进行比较。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> divs = <span class="built_in">document</span>.getElementsByTagName(<span class="string">"div"</span>;),</span><br><span class="line">    i,</span><br><span class="line">    div;</span><br><span class="line"><span class="keyword">for</span>(i = <span class="number">0</span> , len =  divs.length; i &lt; len; i++)&#123;</span><br><span class="line">    div = <span class="built_in">document</span>.createElement(<span class="string">"div"</span>);</span><br><span class="line">    <span class="built_in">document</span>.body.appendChild(div);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h1 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h1><p>DOM由各种节点构成</p>
<ul>
<li>最基本的节点类型是Node，用于抽象地表示文档中一个独立的部分；所有其他类型都继承自Node</li>
<li>Document类型表示整个文档。在JavaScript中，document是Document的一个实例。</li>
<li>Element节点表示文档中所有HTML或XML元素，可以用来操作这些元素的内容和特性</li>
<li>还有一些节点类型，分别表示文本内容，注释、文档类型、CDATA区域和文档片段</li>
</ul>
<p>理解DOM的关键就是理解DOM对性能的影响。DOM操作往往是JavaScript程序中开销最大的部分，因而访问NodeList导致的问题最多。最好的办法是减少DOM操作。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/读书笔记/" rel="tag"># 读书笔记</a>
          
            <a href="/tags/js高程/" rel="tag"># js高程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/08/08/293/" rel="next" title="js高程读书笔记 第七章 函数表达式">
                <i class="fa fa-chevron-left"></i> js高程读书笔记 第七章 函数表达式
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/08/13/298/" rel="prev" title="js高程读书笔记 第十一章 DOM拓展">
                js高程读书笔记 第十一章 DOM拓展 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="何米酥">
            
              <p class="site-author-name" itemprop="name">何米酥</p>
              <p class="site-description motion-element" itemprop="description">Just do...</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">202</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">78</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/hemisu" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/hemisu" target="_blank" title="知乎">
                      
                        <i class="fa fa-fw fa-globe"></i>知乎</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#本章内容"><span class="nav-number">1.</span> <span class="nav-text">本章内容</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#节点层次"><span class="nav-number">2.</span> <span class="nav-text">节点层次</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Node类型"><span class="nav-number">3.</span> <span class="nav-text">Node类型</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Document类型-9"><span class="nav-number">3.1.</span> <span class="nav-text">Document类型 9</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#文档信息"><span class="nav-number">3.1.1.</span> <span class="nav-text">文档信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查找元素"><span class="nav-number">3.1.2.</span> <span class="nav-text">查找元素</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#特殊方法"><span class="nav-number">3.1.3.</span> <span class="nav-text">特殊方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#dom一致性检测"><span class="nav-number">3.1.4.</span> <span class="nav-text">dom一致性检测</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#文档写入"><span class="nav-number">3.1.5.</span> <span class="nav-text">文档写入</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Element类型-1"><span class="nav-number">3.2.</span> <span class="nav-text">Element类型 1</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#HTML元素"><span class="nav-number">3.2.1.</span> <span class="nav-text">HTML元素</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#取得特性"><span class="nav-number">3.2.2.</span> <span class="nav-text">取得特性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#attributes属性"><span class="nav-number">3.2.3.</span> <span class="nav-text">attributes属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建元素"><span class="nav-number">3.2.4.</span> <span class="nav-text">创建元素</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#元素的子节点"><span class="nav-number">3.2.5.</span> <span class="nav-text">元素的子节点</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#text类型-3"><span class="nav-number">3.3.</span> <span class="nav-text">text类型 3</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建文本节点"><span class="nav-number">3.3.1.</span> <span class="nav-text">创建文本节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#规范化文本节点"><span class="nav-number">3.3.2.</span> <span class="nav-text">规范化文本节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#分割文本节点"><span class="nav-number">3.3.3.</span> <span class="nav-text">分割文本节点</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#comment类型-8"><span class="nav-number">3.4.</span> <span class="nav-text">comment类型 8</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#CDATASection-4"><span class="nav-number">3.5.</span> <span class="nav-text">CDATASection 4</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#DocumentType-10"><span class="nav-number">3.6.</span> <span class="nav-text">DocumentType 10</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#DocumentFragment类型-11"><span class="nav-number">3.7.</span> <span class="nav-text">DocumentFragment类型 11</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#attr类型-2"><span class="nav-number">3.8.</span> <span class="nav-text">attr类型 2</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DOM操作技术"><span class="nav-number">4.</span> <span class="nav-text">DOM操作技术</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#动态脚本"><span class="nav-number">4.1.</span> <span class="nav-text">动态脚本</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#动态样式"><span class="nav-number">4.2.</span> <span class="nav-text">动态样式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#操作表格-p283"><span class="nav-number">4.3.</span> <span class="nav-text">操作表格 p283</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用NodeList"><span class="nav-number">4.4.</span> <span class="nav-text">使用NodeList</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#小结"><span class="nav-number">5.</span> <span class="nav-text">小结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">何米酥</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
